package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{

    private CheckBox shuttlecock;
    private CheckBox basketball;
    private CheckBox pingpong;
    private TextView hobby;
    private String hobbys;
    private CharSequence replacement;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();

    }
    private void initView(){
        shuttlecock = findViewById(R.id.like_shuttlecock);
        basketball = findViewById(R.id.like_basketball);
        pingpong = findViewById(R.id.like_pingpong);
        hobby = findViewById(R.id.hobby);
    }
    private void initData(){
        //用来存放被选中的文本信息
        hobbys = new String();


    }
    private void initEvent(){
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpong.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString();//获取被点击的控件的文本信息
        if(b){//判断当前控件是否被选中，选中后将文本信息存放在hobbys中
            if(!hobbys.contains(motion)){//判断当前选中的文本信息是否已经存在hobbys
                hobbys = hobbys+motion;
                hobby.setText(hobbys);
            }
        }else {
            if(hobbys.contains(motion)){
                hobbys=hobbys.replace(motion,"");
                hobby.setText(hobbys);
            }
        }
    }
}
